Repository navigation
fix(logger): child Logger without a level follows its parent level again - #8842
Open
AtulJoshi1206 wants to merge 1 commit into
Open
AtulJoshi1206 wants to merge 1 commit into
AtulJoshi1206 wants to merge 1 commit into
Conversation
Since aws-powertools#5950 a child Logger always called setLevel(), so without an explicit level it resolved to INFO (or the env var) and stopped inheriting the parent's level. DEBUG logs from child modules were lost when the parent used DEBUG, setLevel() or sampling, and INFO logs leaked when the parent used WARNING. A child now only sets a level when one is passed, and otherwise stays NOTSET like before 3.6.0. The same applies to refresh_sample_rate_calculation(), which used to pin a child to INFO on warm invocations. log_level returns the effective level, so a child reports, and copy_config_to_registered_loggers copies, the level the child actually logs at. Explicit child levels (aws-powertools#5950) and the ALC > explicit level > env var priority are unchanged.
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Issue number: closes #8778
Summary
Changes
Since #5950 a child Logger always calls
setLevel(). Without alevelit resolves to INFO (orPOWERTOOLS_LOG_LEVEL) and stops following its parent. This PR keeps #5950's behaviour for children that pass aleveland restores inheritance for children that don't:_init_logger: a child only sets its level when one is passed. Otherwise it staysNOTSET, like before 3.6.0.initial_log_level:NOTSETfor a child without a level. Before this,refresh_sample_rate_calculation()(whichinject_lambda_contextcalls on warm invocations) pinned the child back to INFO.log_level: returns the effective level, so a child reports the level it actually logs at. That also makescopy_config_to_registered_loggers(source_logger=child)copy that level. Without this, the first change alone would makechild.log_leveldrop from 20 to 0. This is the open question from the issue thread. I went with the effective level because returning 0 would change whatlog_levelreports for every default child today, but I'm happy to flip it if you'd rather match 3.5.0 exactly.leveldocstring.The priority doesn't change: ALC, then an explicit level (constructor or
setLevel), thenPOWERTOOLS_LOG_LEVEL. A child with an explicit level still goes through the same resolution, including the ALC warning.User experience
Before (3.6.0 to 3.41.0): with
Logger(level="DEBUG")inapp.pyandLogger(child=True)inshared.py, the child's DEBUG logs were dropped. The same happened withsetLevel("DEBUG")orsampling_rate=1on the parent. With a WARNING parent, the child still emitted INFO.After: the child follows its parent again, as in 3.5.0, and a child with its own
levelstill uses it.Visible differences from the current release:
POWERTOOLS_LOG_LEVEL=DEBUGand an explicitLogger(level="INFO")parent, the child now follows the parent (INFO), as it did in 3.5.0.child.log_levelreturns the inherited level. It's 20 with a default parent, so that case is unchanged.child.levelisNOTSETagain.Verification
New tests: I added 9 functional tests, using the public API only. 7 fail on current
developand pass with the fix. The 2 ALC tests pass on both and guard that ALC still wins for children. No existing test was changed. They cover the cases @saimcyber listed: a parentsetLevel()after the child is created, sampling on a warm invocation, and a child with its own level.Logger suites: on current
develop(Python 3.14),pytest tests/unit/logger tests/functional/loggergives 213 passed.Lint and types:
ruff format --check,ruff checkandmypyare clean.Full suite: on Python 3.12,
pytest tests/unit tests/functional -m "not perf"passed apart fromtest_datadog_write_to_log_with_env_variable. That test also fails in a full run ondevelopand passes on its own, so it's order-dependent and unrelated.Cross-version check: I ran the same two-module app against 3.5.0, 3.6.0, 3.38.0 and the fix, covering:
setLevel, and sampling with the decorator;POWERTOOLS_LOG_LEVEL;AWS_LAMBDA_LOG_LEVELset to WARN, INFO and DEBUG.The branch matches 3.5.0 everywhere, except that fix(logger): child logger must respect log level #5950's explicit child level still works. There's no AWS service integration here, so I didn't deploy it; the ALC paths are covered by setting
AWS_LAMBDA_LOG_LEVEL.AI assistance: an AI coding assistant helped prepare this change; it was reviewed and the tests were run locally.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.